package com.colortea.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.colortea.dao.GoodsGroupTDao;
import com.colortea.entity.GoodsGroupT;
@Repository("goodsGroupTDao")
public class GoodsGroupTDaoImpl extends BaseTDaoImpl<GoodsGroupT> implements GoodsGroupTDao {
	private static final Logger log = LoggerFactory.getLogger(GoodsGroupTDaoImpl.class);
	


	@Override
	public int updateGoodsGroupT(final GoodsGroupT group) {
		log.debug("update goodsGroupT");
		final String queryString="update GoodsGroupT as ggt set ggt.memberprice=:memberprice, ggt.groupprice=:groupprice, ggt.pictureurl=:pictureurl, ggt.sendpoint=:sendpoint,ggt.detail=:detail,ggt.endtime=:endtime,ggt.begintime=:begintime, ggt.goodsname=:goodsname,ggt.cashstate=:cashstate,ggt.cashlimit=:cashlimit,ggt.limitbuy=:limitbuy,ggt.salequantity=:salequantity where ggt.groupid=:groupid";
		try {
			int i = 0;
			Query query = this.getSessionFactory().openSession().createQuery(queryString);
			query.setParameter("goodsname", group.getGoodsname());
			query.setParameter("begintime", group.getBegintime());
			query.setParameter("endtime",group.getEndtime());
			query.setParameter("pictureurl", group.getPictureurl());
			query.setParameter("groupprice", group.getGroupprice());
			query.setParameter("memberprice", group.getMemberprice());
//			query.setParameter("totalordercount", group.getTotalOrderCount());
//			query.setParameter("sordercount", group.getSOrderCount());
			i= query.executeUpdate();
			return i;
		} catch (DataAccessException e) {
			log.debug("update goodsgroupT failed");
			throw e;
		}		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodsGroupT> findAllGoodsGroupT(final int currentPage, final int lineSize) {
		log.debug("find all goodsGroupT");
		try {
			String queryString="from GoodsGroupT order by createtime";
			Query query = this.getSessionFactory().openSession().createQuery(queryString);
			query.setFirstResult((currentPage-1)*lineSize);
			query.setMaxResults(lineSize);
			List list=query.list();
			
			
			if(list.size()>0&&list!=null){
				log.debug("find all success");
				return list;
			}			
			return null;
		} catch (DataAccessException e) {
			log.debug("find all failed");
			throw e;
		}
	}

	@Override
	public int delGoodsGroupT(final String[] list) {	
		log.debug("del goodsgroupt");
		try {
			String queryString="delete from GoodsGroupT as ggt where ggt.groupid=:groupid";
			Query query =  this.getSessionFactory().openSession().createQuery(queryString);
			int i = 0;
			for(String s : list){
				query.setParameter("groupid", s);
				i=query.executeUpdate();
				i++;
			}
			if(list.length==i){
				log.debug("del success");
				return i;
			}else{
				return 0;
			}		
			
			
		} catch (DataAccessException e) {
			log.debug("del failed");
			throw e;
		}
		
		
	}

	@Override
	public int countAllGoodsGroupT() {
		log.debug("countAll GoodsGroupT");
		try {
			String queryString="select count(*) from GoodsGroupT ";
			 Query createQuery = this.getSessionFactory().openSession().createQuery(queryString);
			List list= createQuery.list();
			if(list.size()>0){
				Object o =list.get(0);
				long l =(Long) o;
				return (int)l;
			}			
			return 0;
		} catch (DataAccessException e) {
			log.debug("countAll failed");
			throw e;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodsGroupT> sortAllGoodsGroup(final int currentPage, final int lineSize,
			 final String queryString) {
		try {
			Query query = this.getSessionFactory().openSession().createQuery(queryString);
			query.setFirstResult((currentPage-1)*lineSize);
			query.setMaxResults(lineSize);					
			List list = query.list();
			return list;
		} catch (DataAccessException e) {
			throw e;
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public GoodsGroupT findGoodsGroupById(String groupid) {
		try {
			String queryString="from GoodsGroupT as ggt where ggt.groupid=:groupid ";
			Query createQuery = this.getSessionFactory().openSession().createQuery(queryString);
			createQuery.setParameter("groupid", groupid);
			List<GoodsGroupT> list =  createQuery.list();
			if(!list.isEmpty()){
				return list.get(0);
			}
			return null;
		} catch (DataAccessException e) {
			throw e;
		}
	}

	@Override
	public int updateState(final GoodsGroupT ggt) {
		try {
			final String queryString="update GoodsGroupT as ggt set ggt.state=:state where ggt.groupid=:groupid";
			Query createQuery = this.getSessionFactory().openSession().createQuery(queryString);
			 int i = 0;
			 createQuery.setParameter("groupid", ggt.getGroupid());
			 createQuery.setParameter("state", ggt.getState());
			 i=createQuery.executeUpdate();
			return i;
			
		} catch (DataAccessException e) {
			throw e;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodsGroupT> findGoodsGroupByState(String state) {
		try {
			String queryString="from GoodsGroupT as ggt where ggt.state=:state order by begintime desc";
			Query createQuery = this.getSessionFactory().openSession().createQuery(queryString);
			List<GoodsGroupT> list = createQuery.list();
			if(!list.isEmpty()){
				return list;
			}
			return null;
		} catch (DataAccessException e) {
			throw e;
		}
		
	}

	@Override
	public int updateGoodsGroupHtmlPath(final String htmlpath, final String groupid) {
		try {
			final String queryString="update GoodsGroupT as ggt set ggt.htmlpath=:htmlpath where  ggt.groupid=:groupid";
			Query createQuery = this.getSessionFactory().openSession().createQuery(queryString);
			createQuery.setParameter("htmlpath", htmlpath);
			createQuery.setParameter("groupid", groupid);
			int i=0;
			Query query = this.getSessionFactory().openSession().createQuery(queryString);
			query.setParameter("htmlpath", htmlpath);
			query.setParameter("groupid",groupid);
			i=query.executeUpdate();
			return i;
		} catch (DataAccessException e) {
			throw e;
		}
	}

}
